//
//  Audit.m
//  auditorias
//
//  Created by Mauro on 7/19/13.
//  Copyright (c) 2013 Antilope. All rights reserved.
//

#import "Audit.h"
#import "Base64.h"
#import "ControlVariables.h"
#import "SharedMemory.h"

@implementation Audit

@synthesize internalId;
@synthesize commentText;
@synthesize dateTime;
@synthesize userId;
@synthesize pointOfSellId;
@synthesize pointOfSellInternalId;
@synthesize photoPaths;
@synthesize auditTypeId;
@synthesize hasQuestion;
@synthesize questionAnswer;

- (void) dealloc
{
    [internalId release], internalId = nil;
    [commentText release], commentText = nil;
    [dateTime release], dateTime = nil;
    [userId release], userId = nil;
    [pointOfSellId release], pointOfSellId = nil;
    [pointOfSellInternalId release], pointOfSellInternalId = nil;
    [photoPaths release], photoPaths = nil;
    [auditTypeId release], auditTypeId = nil;
    [super dealloc];
}


- (id) initWithuniqueId: (NSString*) _uniqueId
                 userId:(NSString*) _userId
          pointOfSellId:(NSString*) _pointOfSellId
  pointOfSellInternalId:(NSString*) _pointOfSellInternalId
            commentText:(NSString*) _commentText
            hasQuestion:(bool) _hasQuestion
         questionAnswer:(bool) _questionAnswer
            auditTypeId:(NSString*) _auditTypeId
             photoPaths:(NSArray*) _photoPaths
{
    self = [super init];
    self.internalId = _uniqueId;
    self.dateTime = _uniqueId;
    self.userId = _userId;
    self.pointOfSellId = _pointOfSellId;
    self.pointOfSellInternalId = _pointOfSellInternalId;
    self.commentText = _commentText;
    self.hasQuestion = _hasQuestion;
    self.questionAnswer = _questionAnswer;
    self.auditTypeId = _auditTypeId;
    self.photoPaths = [[[NSArray alloc] initWithArray:_photoPaths] autorelease];
    return self;
}


- (NSString*) toInternalJSON
{
    return [self toJSON];
}

- (NSString*) toJSON
{
    SharedMemory* mem = [SharedMemory sharedInstance];
    NSArray* badCharsArray = [[[NSArray alloc] initWithObjects:@"\\x00-",@"\\x09",@"\\x11",@"\\x12",@"\\x14-",@"\\x1F",@"\\x7F",@"\\n",@"\\r", nil] autorelease];
    
    NSString* imagesString = @"";
    for (int i = 0; i < [self.photoPaths count]; i++) {
        NSString*  path = [self.photoPaths objectAtIndex:i];
        if(path!=nil && ![path isEqualToString:@""]){
            NSString *documentsDirectory = [mem.documentsPath stringByAppendingPathComponent:PHOTO_DIRECTORY];
            NSString *filePath = [documentsDirectory  stringByAppendingPathComponent: path];
            UIImage* image = [UIImage imageWithContentsOfFile:filePath];
            NSData* data = UIImageJPEGRepresentation(image,IMAGE_DEFAULT_COMPRESS_RATE);
            [Base64 initialize];
            NSString *imageEncoded = [Base64 encode:data];
            for (NSString* badChar in badCharsArray) {
                imageEncoded = [imageEncoded stringByReplacingOccurrencesOfString:badChar withString:@""];
            }
            imagesString = [imagesString isEqualToString:@""]? @"" : [NSString stringWithFormat:@"%@,",imagesString];
            imagesString = [NSString stringWithFormat:@"%@\"%@\"",imagesString,imageEncoded];
        }
    }
    
    //NSString* questionString = (!self.hasQuestion)? @"" : [NSString stringWithFormat:@"   \"answer\":%d,",self.questionAnswer];
    NSString* questionString = [NSString stringWithFormat:@"   \"answer\":%d,",((!self.hasQuestion)? true : self.questionAnswer)];
    NSString* pdvAndEnding = ([self.pointOfSellId isEqualToString:@""])? @"" : [Audit getPDVAndEnding:self.pointOfSellId];
    NSString* comment = ([self.commentText isEqualToString:@""])? @"" : [NSString stringWithFormat:@"   \"description\":\"%@\",",self.commentText];
    
    return 	[NSString stringWithFormat:@" {" 
    "\n   \"auditor\":{"
    "\n      \"id\":\"%@\""
    "\n   },"
    "\n   \"auditType\":{"
    "\n      \"id\":\"%@\""
    "\n   },"
    "\n%@"
    "\n%@"
    "\n   \"dateTime\":%@,"
    "\n   \"images\":[ %@ "
    "\n   ],"
    "\n%@ ",self.userId, self.auditTypeId,comment,questionString,self.dateTime,imagesString,pdvAndEnding];
    
}

+ (NSString*) getPDVAndEnding: (NSString*) pointOfSellExternalId
{
    return  [NSString stringWithFormat:@"   \"pointOfSell\":{"
    "\n      \"id\":\"%@\""
    "\n   }  "
    "\n}",pointOfSellExternalId];
}

- (NSString*) getFileName
{
    NSString* midfix = [self.pointOfSellId isEqualToString:@""]? [NSString stringWithFormat:@"%@%@",AUDIT_FILE_MIDFIX_NO_PDV_ID,self.pointOfSellInternalId] : @"";
    return [NSString stringWithFormat:@"%@%@%@",AUDIT_FILE_PREFIX, self.internalId, midfix];
}

//TODO: probar
+ (NSString*) getPDVInternalIdFromPath: (NSString*)  pathAudit
{
    NSRange range = [pathAudit rangeOfString:AUDIT_FILE_MIDFIX_NO_PDV_ID];
    if(range.length==0)
        return @"";
    NSString *substring = [pathAudit substringFromIndex:NSMaxRange(range)] ;
    return substring;
}


@end
